// pages/mine/mine.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    healthStats: {
      totalDays: 365,
      totalSteps: 2856420,
      avgSleep: 7.2,
      healthScore: 85
    },
    menuItems: [
      {
        id: 1,
        icon: '',
        title: '个人资料',
        desc: '编辑个人信息',
        arrow: true
      },
      {
        id: 2,
        icon: '',
        title: '健康报告',
        desc: '查看详细健康数据',
        arrow: true
      },
      {
        id: 3,
        icon: '',
        title: '提醒设置',
        desc: '管理健康提醒',
        arrow: true
      },
      {
        id: 4,
        icon: '',
        title: '目标设定',
        desc: '设置健康目标',
        arrow: true
      },
      {
        id: 5,
        icon: '',
        title: '分享应用',
        desc: '推荐给朋友',
        arrow: true
      },
      {
        id: 6,
        icon: '',
        title: '意见反馈',
        desc: '帮助我们改进',
        arrow: true
      },
      {
        id: 7,
        icon: '',
        title: '关于我们',
        desc: '了解更多信息',
        arrow: true
      }
    ]
  },

  onLoad() {
    this.getUserInfo()
    this.loadHealthStats()
  },

  onShow() {
    // 页面显示时刷新用户信息
    this.getUserInfo()
  },

  getUserInfo() {
    const app = getApp()
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      // 获取用户信息
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: res => {
                app.globalData.userInfo = res.userInfo
                this.setData({
                  userInfo: res.userInfo,
                  hasUserInfo: true
                })
              }
            })
          }
        }
      })
    }
  },

  getUserProfile() {
    wx.getUserProfile({
      desc: '用于完善个人资料',
      success: (res) => {
        const app = getApp()
        app.globalData.userInfo = res.userInfo
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },

  loadHealthStats() {
    // 模拟加载健康统计数据
    // 实际项目中这里应该从服务器获取数据
    const stats = {
      totalDays: Math.floor(Math.random() * 365) + 100,
      totalSteps: Math.floor(Math.random() * 1000000) + 2000000,
      avgSleep: (Math.random() * 2 + 6).toFixed(1),
      healthScore: Math.floor(Math.random() * 20) + 80
    }
    this.setData({
      healthStats: stats
    })
  },

  onMenuItemTap(e) {
    const itemId = e.currentTarget.dataset.id
    const item = this.data.menuItems.find(item => item.id === itemId)
    
    switch(itemId) {
      case 1:
        this.goToProfile()
        break
      case 2:
        this.goToHealthReport()
        break
      case 3:
        this.goToReminder()
        break
      case 4:
        this.goToGoalSetting()
        break
      case 5:
        this.shareApp()
        break
      case 6:
        this.goToFeedback()
        break
      case 7:
        this.goToAbout()
        break
      default:
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        })
    }
  },

  goToProfile() {
    wx.navigateTo({
      url: '/pages/profile/profile'
    })
  },

  goToHealthReport() {
    wx.navigateTo({
      url: '/pages/health-report/health-report'
    })
  },

  goToReminder() {
    wx.navigateTo({
      url: '/pages/reminder/reminder'
    })
  },

  goToGoalSetting() {
    wx.navigateTo({
      url: '/pages/goal-setting/goal-setting'
    })
  },

  shareApp() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  goToFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
  },

  goToAbout() {
    wx.navigateTo({
      url: '/pages/about/about'
    })
  },

  onShareAppMessage() {
    return {
      title: '迷你健康 - 你的健康管理助手',
      path: '/pages/index/index',
      imageUrl: ''
    }
  }
})